#pragma once

#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <iostream>
#include "Common.hpp"
class InetAddr
{
private:
    void PortNetToHost()
    {
        _port = ntohs(_net_addr.sin_port);
    }
    void IpNetToHost()
    {
        char buffer[64];
         const char *ip = ::inet_ntop(AF_INET, &_net_addr.sin_addr, buffer, sizeof(buffer));
        (void)ip;
        _ip=buffer;
    }

public:
    InetAddr(const struct sockaddr_in &net_addr)
        : _net_addr(net_addr)
    {
        PortNetToHost();
        IpNetToHost();
    }
    InetAddr(uint16_t port) : _port(port), _ip("")
    {
        _net_addr.sin_family = AF_INET;
        _net_addr.sin_addr.s_addr = INADDR_ANY;
        _net_addr.sin_port = ::htons(port);
    }
    bool operator==(const InetAddr& addr)
    {
        return _ip==addr._ip && _port==addr._port;
    }
    uint16_t GetPort() { return _port; }
    std::string GetIp() { return _ip; }
    struct sockaddr *GetNetAddrPtr() { return CONV(&_net_addr); }
    socklen_t GetNetAddrLen() { return sizeof(_net_addr); }
    std::string GetAddr() {return GetIp()+":"+std::to_string(_port);}
    ~InetAddr()
    {
    }

private:
    struct sockaddr_in _net_addr;
    uint16_t _port;
    std::string _ip;
};